

<?php

class BlockController extends Zend_Controller_Action {

    public function __construct(Zend_Controller_Request_Abstract $request, Zend_Controller_Response_Abstract $response, array $invokeArgs = array()) {
        parent::__construct($request, $response, $invokeArgs);
    }

    public function preDispatch() {
        $auth = Zend_Auth::getInstance();
        if (!$auth->hasIdentity()) {
            if ($this->_request->getActionName() != 'admin') {
                $this->_redirect('admin');
            }
        }
    }

    public function indexAction() {
        $this->buildHead();
        $Block = new Model_Block();
        $data = $Block->getAllBlock();
        $this->view->block = $data;
    }

    public function addAction() {
        $lon = $this->_request->getParam("lon");
        $lat = $this->_request->getParam("lat");
        $description = $this->_request->getParam("description");
        $this->_helper->viewRenderer->setNoRender(true);
        $Block = new Model_Block();
        $result = $Block->insertNewBlock($lon, $lat,$description);
        if ($result != null) {
            echo $this->returnDone();
        } else {
            echo $this->returnFail();
        }
    }

    public function deleteAction() {
        $id = $this->_request->getParam("id");
        $this->_helper->viewRenderer->setNoRender(true);
        $Block = new Model_Block();
        $result = $Block->deleteBlock($id);
        if ($result != null) {
            echo $this->returnDone();
        } else {
            echo $this->returnFail();
        }
    }

    public function updateAction() {
        $id = $this->_request->getParam("id");
        $lon = $this->_request->getParam("lon");
        $lat = $this->_request->getParam("lat");
        $description = $this->_request->getParam("description");
        $this->_helper->viewRenderer->setNoRender(true);
        $Block = new Model_Block();
        $result = $Block->updateBlock($id, $lon, $lat,$description);
        if ($result != null) {
            echo $this->returnDone();
        } else {
            echo $this->returnFail();
        }
    }

    private function returnDone() {
        $array = array(
            "errorCode" => 0,
            "errorMessage" => "Query completed.",
        );
        echo json_encode($array);
    }

    private function returnFail() {
        $array = array(
            "errorCode" => -1,
            "errorMessage" => "Query fail.",
        );
        echo json_encode($array);
    }

    private function buildHead() {
        $base = $this->_request->getBaseUrl();
        $base = $this->_request->getBaseUrl();
        $this->view->base = $base;
        $this->view->update = $base . "/Block/update";
        $this->view->delete = $base . "/Block/delete";
        $this->view->add = $base . "/Block/add";
        $this->view->headMeta()->appendHttpEquiv('Content-Type', 'text/html;charset=utf-8');
        $this->view->headScript()->appendFile($base . "/js/Ajax.js", 'text/javascript');
        $this->view->headScript()->appendFile($base . "/js/sunnyAjax.js", 'text/javascript');
    }

}
?>

